<?php

/**
 * 验证码处理
 */
class CheckcodeAction extends Action {

    public function index() {
        import("Util.Checkcode", LIB_PATH);
        $checkcode = new Checkcode();
        if (isset($_GET['len']) && intval($_GET['len']))
            $checkcode->code_len = intval($_GET['len']);
        if ($checkcode->code_len > 8 || $checkcode->code_len < 2) {
            $checkcode->code_len = 4;
        }
        //设置验证码字符库
        if(isset($_GET['charset'])){
        	$checkcode->charset = trim($_GET['charset']);
        }
        //强制验证码不得小于4位
        if($checkcode->code_len < 4){
            $checkcode->code_len = 4;
        }
        if (isset($_GET['size']) && intval($_GET['size']))
            $checkcode->font_size = intval($_GET['size']);
        if (isset($_GET['w']) && intval($_GET['w']))
            $checkcode->width = intval($_GET['w']);
        if ($checkcode->width <= 0 || $checkcode->width > 200) {
            $checkcode->width = 130;
        }
        if (isset($_GET['h']) && intval($_GET['h']))
            $checkcode->height = intval($_GET['h']);
        if ($checkcode->height <= 0 || $checkcode->height > 100) {
            $checkcode->height = 50;
        }
        if (isset($_GET['font_color']) && trim(urldecode($_GET['font_color'])) && preg_match('/(^#[a-z0-9]{6}$)/im', trim(urldecode($_GET['font_color']))))
            $checkcode->font_color = trim(urldecode($_GET['font_color']));
        if (isset($_GET['background']) && trim(urldecode($_GET['background'])) && preg_match('/(^#[a-z0-9]{6}$)/im', trim(urldecode($_GET['background']))))
            $checkcode->background = trim(urldecode($_GET['background']));
        $checkcode->doimage();
        
        //验证码类型
        $type = $this->_get("name");
        $type = $type?strtolower($type):"verify";
        $verify = session("_verify_");
        if(empty($verify)){
            $verify = array();
        }
        $verify[$type] = $checkcode->get_code();
        session("_verify_",$verify);
    }


    public function set(){
        import("Util.Checkcode", LIB_PATH);
        $checkcode = new Checkcode();

        $param = $_GET['param']?$_GET['param']:'4_16_100_40_verify';
        $params = explode('_', $param);
        if(count($params) != 5){
            $param  = '4_16_100_40_verify';
            $params = explode('_', $param);
        }
        // p($params);
        // 长度
        $checkcode->code_len = $params[0];
        if ($checkcode->code_len > 8 || $checkcode->code_len < 4) {
            $checkcode->code_len = 4;
        }
        // 大小
        $checkcode->font_size = $params[1];
        if ($checkcode->font_size > 24 || $checkcode->font_size < 12) {
            $checkcode->font_size = 14;
        }
        // 宽
        $checkcode->width = $params[2];
        if ($checkcode->width <= 0 || $checkcode->width > 200) {
            $checkcode->width = 130;
        }
        // 高
        $checkcode->height = $params[3];
        if ($checkcode->height <= 0 || $checkcode->height > 100) {
            $checkcode->height = 50;
        }
        // font color
        // background color
        $checkcode->doimage();
        
        // Name
        $type = $params[4];
        $type = $type?strtolower($type):"verify";
        $verify = $checkcode->get_code();
        session($type,$verify);
    }

}

?>